<?PHP
session_start();
/* $Id: addFlags.php 7 2009-03-24 20:25:52Z ronan $ */
//edited by Slyferz

include 'includes/database.php';
include 'includes/qualityq.php';

//converting the timezone numbers to words
function timezone($zone)
{
	if($zone == 1)
		return "European";
	else if($zone == 2)
		return "American";
	else if($zone == 3)
		return "Oceanic";
	return 0;
}

function updateFlags() {
	$id = $_SESSION['userid'];
	$charID = addslashes($_POST['charID']);
	$result = mysql_query("SELECT * FROM characters WHERE charID = '".$charID."'");
	$row = mysql_fetch_array($result);
	$extras['dread'] = $row['dread'];
	$extras['carrier'] = $row['carrier'];
	$extras['tz'] = $row['tz'];
	$name = $row['name'];
	$name = str_replace("'","c39c",$name);
	$name = str_replace('"',"c34c",$name);
	if ($_POST['afk'] == "true"){ 
		$_POST['afk'] = 1;
		if($row['afk'] == 0)
			query($id, "set ".$name." to afk");
	}		
	else{
		$_POST['afk'] = 0;
		$_POST['afkText'] = NULL;
		if($row['afk'] == 1)
			query($id, "unset ".$name." from afk");
	}
	if ($_POST['investigate'] != "true"){
		$_POST['investigate'] = 0;
		if($row['investigate'] == 1)
			query($id,"unset ".$name." from investigate");
	}
	else{
		$_POST['investigate'] = 1;
		if($row['investigate'] == 0)
			query($id,"set ".$name." to investigate");
	}       
	if ($_POST['posgunner'] != "true"){
		$_POST['posgunner'] = 0;
		if($row['posgunner'] == 1)
			query($id,"unset ".$name." from posgunner");
	}
	else{
		$_POST['posgunner'] = 1;
		if($row['posgunner'] == 0)
			query($id,"set ".$name." to posgunner");
	}       
	$_POST['notes'] = nl2br($_POST['notes']);

	$afk = addslashes($_POST['afk']);
	$afkText = addslashes($_POST['afkText']);
	$tz = addslashes($_POST['tz']);
	$carrier = addslashes($_POST['carrier']);
	$dread = addslashes($_POST['dread']);
	$investigate = addslashes($_POST['investigate']);
	$posgunner = addslashes($_POST['posgunner']);
	$notes = addslashes($_POST['notes']);
	$posd = addslashes($_POST['posd']);
	$exempt = addslashes($_POST['exempt']);
	$legacy = addslashes($_POST['legacy']);
	$probation = addslashes($_POST['probation']);
	
	$result = mysql_query("SELECT * FROM Jobs WHERE charID = '".$charID."'") or die(mysql_error());
	$row = mysql_fetch_array($result);
	if($posd != 'true'){
		$posd = 0;
		if($row['pos'] == 1)
			query($id,"unset ".$name." from pos duties");
	}
	else{
		$posd = 1;
		if($row['pos'] == 0)
			query($id,"set ".$name." to pos duties");
	}       
	if($exempt != 'true'){
		$exempt = 0;
		if($row['exempt'] == 1)
			query($id,"unset ".$name." from exempt");
	}
	else{
		$exempt = 1;
		if($row['exempt'] == 0)
			query($id,"set ".$name." to exempt");
	}       
	if($legacy != 'true'){
		$legacy = 0;
		if($row['legacy'] == 1)
			query($id,"unset ".$name." from legacy member");
	}
	else{
		$legacy = 1;
		if($row['legacy'] == 0)
			query($id,"set ".$name." to legacy member");
	}       
	if($probation != 'true'){
		$probation = 0;
		if($row['probation'] == 1)
			query($id,"unset ".$name." from probation");
	}
	else{
		$probation = 1;
		if($row['probation'] == 0)
			query($id,"set ".$name." to probation");
	}
	//cap info
	//if dread was de-selected and there was a dread previously
	if($dread != true && !empty($extras['dread']))
		query($id,"unset ".$name." from dread pilot");
	//if dread was selected and there was nothing previously
	else if($dread == true && $extras['dread'] == 0)
		query($id,"set ".$name." to dread pilot");
	if($carrier != true && !empty($extras['carrier']))
		query($id,"unset ".$name." from carrier pilot");
	else if($carrier == true && $extras['carrier'] == 0)
		query($id,"set ".$name." to carrier pilot");
	if($tz != true && !empty($extras['tz']))
		query($id,"unset ".$name." from ".timezone($extras['tz'])." timezone");
	else if($tz == true && $extras['tz'] == 0)
		query($id,"set ".$name." to ".timezone($tz)." timezone");

	$query = "UPDATE characters SET
		afk = '".$afk."',
		afkText = '".$afkText."',
		tz = '".$tz."',
		carrier = '".$carrier."',
		dread = '".$dread."',
		investigate = '".$investigate."',
		posgunner = '".$posgunner."',
		notes = '".$notes."'
		WHERE charID = '".$charID."'";
	$result = mysql_query($query);

	if($result)
	{
		//inserting the data into the job table, making sure that the table
		//will not be full of 0's due to being afk
		$result = mysql_query("SELECT * FROM Jobs WHERE charID = ".$charID);
		$row = mysql_fetch_array($result);
		if(!empty($row[0]))
		{
			$query = "UPDATE Jobs SET
			pos = '".$posd."',
			exempt = '".$exempt."',
			legacy = '".$legacy."',
			probation = '".$probation."'
			WHERE charID = '".$charID."'";
			$result = mysql_query($query) or die(mysql_error());
			if($posd != 1 && $exempt != 1 && $legacy != 1 && $probation != 1)
			{
				mysql_query("DELETE FROM Jobs WHERE charID = '".$charID."'");
				$result = 1;
			}
		}
		else if(!($posd != 1 && $exempt != 1 && $legacy != 1 && $probation != 1))
		{
			mysql_query("INSERT INTO Jobs VALUES ('".$charID."', '".$posd."','".$exempt."','".$legacy."','".$probation."')") or die(mysql_error());
			$result = 1;
		}
	}
	$main = addslashes($_POST['main2']);
	if ($_POST['redirect'] == '/search.php') $_POST['redirect'] = '/index.php';
	if ($_POST['redirect'] == '/characters.php') $_POST['redirect'] = "/characters.php?id=".$main;
	if ($result) header("Location: ".$_POST['redirect']);
}

updateFlags();
?>
